package io.sage.controller;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.sage.common.utils.FileName;
import io.sage.common.utils.R;
import io.sage.service.QnUploadService;


@RestController
@RequestMapping("/admin/xyz")
public class SysOssController {

	@Autowired
	private QnUploadService qnUploadService;
	
	
	 /**
     * 上传文件到七牛云存储
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/qiniu")
    @ResponseBody
    public R uploadImgQiniu(@RequestParam("file") MultipartFile multipartFile,HttpServletRequest request) throws IOException {

        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        
        String user = request.getParameter("user");
        System.out.println(user);
//        String path = uploadQNImg(inputStream, suffix); // KeyUtil.genUniqueKey()生成图片的随机名
//        filename文件的名称. 业务字段会使用.
//        String filename = multipartFile.getOriginalFilename();
        String fileName2 = FileName.getFileName(suffix);
        String uploadFile = qnUploadService.uploadFile(inputStream, fileName2);
        
        
        return R.ok(uploadFile);
    }
    
	
}
